package com.ruoyi.dishes.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dishes.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.dishes.domain.TbRaw;
import com.ruoyi.dishes.service.ITbRawService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 原料Controller
 * 
 * @author ruoyi
 * @date 2023-04-24
 */
@RestController
@RequestMapping("/raw")
public class TbRawController extends BaseController
{
    @Autowired
    private ITbRawService tbRawService;

    /**
     * 查询原料列表
     */
    @RequiresPermissions("dishes:raw:list")
    @GetMapping("/list")
    public TableDataInfo list(TbRaw tbRaw)
    {
        startPage();
        List<TbRaw> list = tbRawService.selectTbRawList(tbRaw);
        return getDataTable(list);
    }

    /**
     * 导出原料列表
     */
    @RequiresPermissions("dishes:raw:export")
    @Log(title = "原料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRaw tbRaw)
    {
        List<TbRaw> list = tbRawService.selectTbRawList(tbRaw);
        ExcelUtil<TbRaw> util = new ExcelUtil<TbRaw>(TbRaw.class);
        util.exportExcel(response, list, "原料数据");
    }

    /**
     * 获取原料详细信息
     */
    @RequiresPermissions("dishes:raw:query")
    @GetMapping(value = "/{rawId}")
    public AjaxResult getInfo(@PathVariable("rawId") Long rawId)
    {
        return success(tbRawService.selectTbRawByRawId(rawId));
    }

    /**
     * 新增原料
     */
    @RequiresPermissions("dishes:raw:add")
    @Log(title = "原料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRaw tbRaw)
    {

        IdWorker idWorker = new IdWorker();
        tbRaw.setRawId(idWorker.nextId());

        return toAjax(tbRawService.insertTbRaw(tbRaw));
    }

    /**
     * 修改原料
     */
    @RequiresPermissions("dishes:raw:edit")
    @Log(title = "原料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRaw tbRaw)
    {
        return toAjax(tbRawService.updateTbRaw(tbRaw));
    }

    /**
     * 删除原料
     */
    @RequiresPermissions("dishes:raw:remove")
    @Log(title = "原料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rawIds}")
    public AjaxResult remove(@PathVariable Long[] rawIds)
    {
        return toAjax(tbRawService.deleteTbRawByRawIds(rawIds));
    }
}
